package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    private int orderId = 0;
    @Override
    public int saveOrder(Orders orders) throws SQLException{
        String sql = "insert into orders(userId,businessId,orderDate,orderTotal,daId,orderState) values(?,?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            pst.setString(1,orders.getUserId());
            pst.setInt(2,orders.getBusinessId());
            pst.setString(3,orders.getOrderDate());
            pst.setString(4,orders.getOrderTotal());
            pst.setInt(5,orders.getDaId());
            pst.setInt(6,orders.getOrderState());
            pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            while(rs.next()){
                orderId= rs.getInt(1);
        }
        }finally {
            DBUtil.close(pst);
        }
        return orderId;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws SQLException{
        Orders orders = new Orders();
        List<Orderdetailet> list = new ArrayList<>();
        String sql = "SELECT * from orders,business,orderdetailet,food where orders.businessId=business.businessId and orders.orderId=orderdetailet.orderId and food.foodId=orderdetailet.foodId and orders.orderId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,orderId);
            rs = pst.executeQuery();
            while (rs.next()){
                Food food = new Food();
                Business business = new Business();
                Orderdetailet orderdetailet = new Orderdetailet();

                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getString("orderTotal"));
                orders.setDaId(rs.getInt(rs.getInt("daId")));
                orders.setOrderState(rs.getInt("orderState"));

                orderdetailet.setOdId(rs.getInt("odId"));
                orderdetailet.setOrderId(rs.getInt("orderId"));
                orderdetailet.setFoodId(rs.getInt("foodId"));
                orderdetailet.setQuantity(rs.getInt("quantity"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                orderdetailet.setFood(food);
                list.add(orderdetailet);
                orders.setList(list);
                orders.setBusiness(business);
            }
        } finally {
            DBUtil.close(pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws SQLException{
        List<Orders> list = new ArrayList<>();
        String sql = "SELECT * from orders,business where orders.businessId=business.businessId and orders.userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1,userId);
            rs = pst.executeQuery();
            while (rs.next()){
                Orders orders = new Orders();
                Business business = new Business();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderId(rs.getInt("orderId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getString("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                orders.setBusiness(business);
                list.add(orders);

            }
        } finally {
            DBUtil.close(pst);
        }
        return list;
    }
}
